package com.wgchao.authmanager.infrastructure.convert.ev;

import com.wgchao.authmanager.application.vo.RoleVo;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.AgencyInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.RoleInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.UserInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.AgencyInfoJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.UserInfoJpaReposotory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: RoleInfoConvertVo
 * @author: wgchao
 * @createTime: 2020/5/7 4:04 PM
 * @Description: TODO
 */
@Component
public class RoleInfoConvertVo implements Convert<RoleVo, RoleInfo> {
    @Autowired
    private UserInfoJpaReposotory userInfoJpaReposotory;
    @Autowired
    private AgencyInfoJpaRepository agencyInfoJpaRepository;

    @Override
    public RoleInfo toEntity(RoleVo roleInfoVo) {
        return null;
    }

    @Override
    public RoleVo toModel(RoleInfo roleInfo) {
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(roleInfo, roleVo);
        UserInfo creator = userInfoJpaReposotory.findFirstByUname(roleInfo.getCreateUser());
        if(creator != null){
            roleVo.setCreatorName(creator.getName());
        }
        if(!StringUtils.isEmpty(roleInfo.getLastUpdateUser())){
            UserInfo updator = userInfoJpaReposotory.findFirstByUname(roleInfo.getLastUpdateUser());
            if(updator != null){
                roleVo.setLastUpdatorName(updator.getName());
            }
        }
        AgencyInfo agencyInfo = agencyInfoJpaRepository.findFirstByAgencyCode(roleInfo.getAgencyCode());
        roleVo.setAgencyName(agencyInfo.getAgencyName());

        return roleVo;
    }
}
